//
//  TTRTCManager.swift
//  TTBLL
//
//  Created by Toj on 4/19/21.
//

import Foundation

class TTRTCManager: NSObject {

    private override init() { }
    public static let shared = TTRTCManager()

    // 本地即将进入回调
    public var localWillJoinBlock: TTRTCLocalWillJoin? {
        didSet {
            server.localWillJoinBlock = localWillJoinBlock
        }
    }
    // 本地已经进入回调
    public var localDidJoinedBlock: TTRTCLocalDidJoined? {
        didSet {
            server.localDidJoinedBlock = localDidJoinedBlock
        }
    }
    // 本地重新进入回调
    public var localReJoinedBlock: TTRTCLocalDidReJoined? {
        didSet {
            server.localReJoinedBlock = localReJoinedBlock
        }
    }
    // 本地第一帧回调
    public var localFirstVideoDecodedBlock: TTRTCFirstVideoDecoded? {
        didSet {
            server.localFirstVideoDecodedBlock = localFirstVideoDecodedBlock
        }
    }
    
    // 远端进入回调
    public var remoteDidJoinedBlock: TTRTCRemoteDidJoined? {
        didSet {
            server.remoteDidJoinedBlock = remoteDidJoinedBlock
        }
    }
    // 远端第一帧回调
    public var remoteFirstVideoDecodedBlock: TTRTCFirstVideoDecoded? {
        didSet {
            server.remoteFirstVideoDecodedBlock = remoteFirstVideoDecodedBlock
        }
    }
    
    // 视频(关闭/打开)回调
    public var didVideoMutedBlock: TTRTCDidStreamMuted? {
        didSet {
            server.didVideoMutedBlock = didVideoMutedBlock
        }
    }
    // 音频(关闭/打开)回调
    public var didAudioMutedBlock: TTRTCDidStreamMuted? {
        didSet {
            server.didAudioMutedBlock = didAudioMutedBlock
        }
    }
    // 视频质量回调
    public var videoNetworkQualityBlock: TTRTCVideoNetworkQuality? {
        didSet {
            server.videoNetworkQualityBlock = videoNetworkQualityBlock
        }
    }
    // 音频质量回调
    public var audioVolumeIndicationBlock: TTRTCAudioVolumeIndication? {
        didSet {
            server.audioVolumeIndicationBlock = audioVolumeIndicationBlock
        }
    }
    
    // 有用户离开
    public var remoteDidOfflineBlock: TTRTCDidOffline? {
        didSet {
            server.remoteDidOfflineBlock = remoteDidOfflineBlock
        }
    }
    
    // Token已经失效
    public var tokenDidTimeoutBlock: TTRTCDidTimeout? {
        didSet {
            server.tokenDidTimeoutBlock = tokenDidTimeoutBlock
        }
    }
    // 发生错误回调
    public var didOccurErrorBlock: TTRTCDidOccurError? {
        didSet {
            server.didOccurErrorBlock = didOccurErrorBlock
        }
    }
    // 接收对方数据流消息错误的回调
    public var didOccurStreamMessageErrorBlock: TTRTCDidOccurStreamMessageError? {
        didSet {
            server.didOccurStreamMessageErrorBlock = didOccurStreamMessageErrorBlock
        }
    }
    
    private lazy var server = TTRTCServer.shared
    private var room: TTLiveRoom?
    
    public func binding(room: TTLiveRoom) {
        self.room = room
        guard let rtc = room.rtc else { return }
        server.setupService(rtc: rtc)
    }
}

extension TTRTCManager {
    public func joinLiveRoom(userID: TTUInt, roomID: String, local view: TTView) {
        server.joinLiveRoom(userID: userID, roomID: roomID, local: view)
    }
    
    public func quitLiveRoom(complete quitBlock: TTRTCQuitLiveRoom?) {
        server.quitLiveRoom(complete: quitBlock)
    }
    
    public func switchRemoteVideoStream(uid: TTUInt, type: TTVideoStreamType) {
        server.switchRemoteVideoStream(uid: uid, type: type)
    }
    
    public func localVideo(enabled: Bool) {
        server.localVideo(enabled: enabled)
    }
    
    public func pushLocalVideoStream(muted: Bool) {
        server.pushLocalVideoStream(muted: muted)
    }
    
    public func pushLocalAudioStream(muted: Bool) {
        server.pushLocalAudioStream(muted: muted)
    }
    
    public func pullRemoteVideoStream(uid: TTUInt, muted: Bool) {
        server.pullRemoteVideoStream(uid: uid, muted: muted)
    }
    
    public func pullRemoteAudioStream(uid: TTUInt, muted: Bool) {
        server.pullRemoteAudioStream(uid: uid, muted: muted)
    }
    
    public func pullRemoteVideoStreamAll(muted: Bool) {
        server.pullRemoteVideoStreamAll(muted: muted)
    }
    
    public func pullRemoteAudioStreamAll(muted: Bool) {
        server.pullRemoteAudioStreamAll(muted: muted)
    }
    
    public func beautyEffect(enable: Bool) {
        server.beautyEffect(enable: enable)
    }
    
    #if canImport(Cocoa)
    public func sharedFullScreen() {
        server.sharedFullScreen()
    }
    
    public func sharedScreenWindowId(windowId: TTUInt, size: CGSize) {
        server.sharedScreenWindowId(windowId: windowId, size: size)
    }
    
    public func sharedScreenDisplayId(displayId: TTUInt, size: CGSize) {
        server.sharedScreenDisplayId(displayId: displayId, size: size)
    }
    
    public func stopScreenCapture() {
        server.stopScreenCapture()
    }
    #endif
}

//extension TTRTCManager {
//    private func setupMakeService(service: TTRTCService) {
//        server.localWillJoinBlock = self.localWillJoinBlock
//        server.localDidJoinedBlock = self.localDidJoinedBlock
//        server.localReJoinedBlock = self.localReJoinedBlock
//        server.localFirstVideoDecodedBlock = self.localFirstVideoDecodedBlock
//        server.remoteDidJoinedBlock = self.remoteDidJoinedBlock
//        server.remoteFirstVideoDecodedBlock = self.remoteFirstVideoDecodedBlock
//        server.didVideoMutedBlock = self.didVideoMutedBlock
//        server.didAudioMutedBlock = self.didAudioMutedBlock
//        server.videoNetworkQualityBlock = self.videoNetworkQualityBlock
//        server.audioVolumeIndicationBlock = self.audioVolumeIndicationBlock
//        server.remoteDidOfflineBlock = self.remoteDidOfflineBlock
//        server.tokenDidTimeoutBlock = self.tokenDidTimeoutBlock
//        server.didOccurErrorBlock = self.didOccurErrorBlock
//        server.didOccurStreamMessageErrorBlock = self.didOccurStreamMessageErrorBlock
//    }
//}
